package cn.edu.bit.learn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.edu.bit.learn.form.StudentForm;
import cn.edu.bit.learn.tool.DbManager;

public class StudentDAO {
	public boolean login(StudentForm sf){
		DbManager dm = new DbManager();
		Connection conn =dm.getCon();
		String sql="select name,password from manager where name=? and password=?";
		//System.out.println(sql+"-------------------");
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, sf.getName());
			pstm.setString(2, sf.getPassword());
			rs=pstm.executeQuery();
			if(rs!=null){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dm.closeResult(rs);
			dm.clostStmt(pstm);
			dm.closeConn(conn);
		}
		return false;
		
	}
	public boolean add(StudentForm form){
		DbManager dm = new DbManager();
		Connection conn = dm.getCon();
		String sql="insert into student values(?,?,?,?,?,?,?)";
		PreparedStatement pstm=null;
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, form.getStudentNo());
			pstm.setString(2, form.getPassword());
			pstm.setString(3, form.getName());
			pstm.setString(4, form.getGender());
			pstm.setString(5, form.getClasss());
			pstm.setString(6, form.getDepartment());
			pstm.setString(7, form.getPermitted());
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dm.clostStmt(pstm);
			dm.closeConn(conn);
		}
		
		return false;
	}
}
